/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.sec.content.role;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Role;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.sec.SecurityContentConstants;
import han.bifang.gwt.client.sec.SecurityMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class CreateRoleContent extends Content {
	private SecurityMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private VerticalPanel panel;

	private TextBox roleName;
	private TextArea desc;
	
	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

//			String id = (String)res.getResponse();
//			HashMap p = new HashMap();
//			p.put("id", id);
//			listener.onContentChanged(
//					SecurityContentConstants.CREATE_USER,
//					SecurityContentConstants.USER_DETAIL, p);
			roleName.setText(null);
			desc.setText(null);
			Window.alert(cons.createOk());
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public CreateRoleContent(SecurityMessages cons,
			BifangGWTServiceAsync service, GwtContext context) {
		this.service = service;
		this.context = context;
		this.cons = cons;

		panel = new VerticalPanel();
		panel.setSpacing(5);
		initWidget(panel);

	}

	public void onLoad() {
		Grid table = new Grid(2, 2);
		table.setWidth("60%");
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "30%");
		cf.setWidth(1, "70%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		
		table.setHTML(0, 0, cons.roleName());
		table.setHTML(1, 0, cons.description());

		roleName = new TextBox();
		roleName.setVisibleLength(32);

		
		desc = new TextArea();
		desc.setCharacterWidth(64);
		desc.setVisibleLines(6);

		table.setWidget(0, 1, roleName);
		table.setWidget(1, 1, desc);

		panel.add(table);

		HorizontalPanel h = new HorizontalPanel();
		
		Button b = new Button(cons.create());
		b.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				if(!validate()){
					return;
				}
				Role u = new Role();
				u.setDescription(desc.getText());
				u.setRoleName(roleName.getText());
				u.setRoleType(EntityConstants.TYPE_ACCESS);
				
				
				RequestObject req = new RequestObject();
				req
						.setAction("han.bifang.gwt.server.action.sec.role.CreateRoleAction");
				req.setRequest(u);
				
				service.call(req, cb);
			}
		});
		panel.add(new HTML("&nbsp;"));
		
		Button b1 = new Button(cons.ret());
		b1.addClickListener(new ClickListener(){

			
			public void onClick(Widget sender) {
				listener.onContentChanged(SecurityContentConstants.CREATE_ROLE,
						SecurityContentConstants.ROLE_MGMT, null);
				
			}
			
		});
		h.add(b);
		h.add(new HTML("&nbsp;&nbsp;"));
		h.add(b1);
		
		panel.add(h);
	}
	
	private boolean validate() {
		if (!StringUtils.checkLenght(roleName.getText(), 1, 32)) {
			Window.alert(cons.lengthError(cons.loginName(), "1", "32"));
			return false;
		}
		
		if(! StringUtils.lengthLess(desc.getText(), 1024)){
			Window.alert(cons.lengthError(cons.description(), "0", "1024"));
			return false;
		}
		
		return true;

	}

}
